﻿using Leeder.Logger;
using LeederGlobal;
using SPSAndong.Business;
using SPSAndong.Comm;
using SPSAndong.Model;
using Sunny.UI;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace SPSAndong.Forms
{
    public partial class FrmProjectSet : UIPage
    {
        public LogOperate LogObj => Log.GetLogger(this.GetType());
        /// <summary>
        /// 判断窗体是否首次加载
        /// </summary>
        bool isLoad = true;
        /// <summary>
        /// 软件运行时实时选中的列表框
        /// </summary>
        UIDataGridView dataGridView;

        ProjectSetBLL projectSetBLL = new ProjectSetBLL();

        public FrmProjectSet()
        {
            InitializeComponent();
        }

        private void FrmProjectSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            tabPage1.ForeColor = Color.Black;

            dgv1.AutoGenerateColumns = false;
            dgv1.AllowUserToAddRows = false;
            dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv1.ReadOnly = true;
            dgv1.AddColumn("序号", "ID", 10);
            dgv1.AddColumn("岗位工程名称", "WorkName", 35);
            dgv1.AddColumn("信号源", "SignalMark", 20);

            dgv2.AutoGenerateColumns = false;
            dgv2.AllowUserToAddRows = false;
            dgv2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv2.ReadOnly = true;
            dgv2.AddColumn("序号", "ID", 10);
            dgv2.AddColumn("岗位工程名称", "WorkName", 35);
            dgv2.AddColumn("信号源", "SignalMark", 20);

            dgv3.AutoGenerateColumns = false;
            dgv3.AllowUserToAddRows = false;
            dgv3.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv3.ReadOnly = true;
            dgv3.AddColumn("序号", "ID", 10);
            dgv3.AddColumn("岗位工程名称", "WorkName", 35);
            dgv3.AddColumn("信号源", "SignalMark", 20);

            dgv4.AutoGenerateColumns = false;
            dgv4.AllowUserToAddRows = false;
            dgv4.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv4.ReadOnly = true;
            dgv4.AddColumn("序号", "ID", 10);
            dgv4.AddColumn("岗位工程名称", "WorkName", 35);
            dgv4.AddColumn("信号源", "SignalMark", 20);

            dgv5.AutoGenerateColumns = false;
            dgv5.AllowUserToAddRows = false;
            dgv5.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv5.ReadOnly = true;
            dgv5.AddColumn("序号", "ID", 10);
            dgv5.AddColumn("岗位工程名称", "WorkName", 35);
            dgv5.AddColumn("信号源", "SignalMark", 20);

            LoadDgv();
        }

        private void LoadDgv()
        {
            var list = new ProjectSetBLL().GetModelAll();
            dgv1.DataSource = null;
            dgv2.DataSource = null;
            dgv3.DataSource = null;
            dgv5.DataSource = null;
            dgv1.DataSource = list.Where(s => s.AreaType == 1).ToList();
            dgv2.DataSource = list.Where(s => s.AreaType == 2).ToList();
            dgv3.DataSource = list.Where(s => s.AreaType == 3).ToList();
            dgv4.DataSource = list.Where(s => s.AreaType == 4).ToList();
            dgv5.DataSource = list.Where(s => s.AreaType == 5).ToList();
        }

        private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView = (UIDataGridView)sender;
                    //若行已是选中状态就不再进行设置
                    if (dataGridView.Rows[e.RowIndex].Selected == false)
                    {
                        dataGridView.ClearSelection();
                        dataGridView.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (dataGridView.SelectedRows.Count == 1)
                    {
                        dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    uiContextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        /// <summary>
        /// 开捆设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            dataGridView = (UIDataGridView)sender;
            ProjectEdit(dataGridView.Name, dataGridView.CurrentRow.DataBoundItem as ProjectSetModel);
        }

        private void toolStripMenuItemUpdata_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            ProjectEdit(dataGridView.Name, dataGridView.CurrentRow.DataBoundItem as ProjectSetModel);
        }

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            ProjectEdit(dataGridView.Name);
        }

        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            if (UIMessageDialog.ShowMessageDialog("您确定要删除该工程吗？", UILocalize.AskTitle, true, UIStyle.Red, true, Frame?.TopMost ?? false))
            {
                ProjectDelect(dataGridView.CurrentRow.DataBoundItem as ProjectSetModel);
            }
        }

        /// <summary>
        /// 工程编辑
        /// </summary>
        /// <param name="model">工程模型</param>
        private void ProjectEdit(string dgvName, ProjectSetModel model = null)
        {
            var triaGroups = GolbalVariable.TriaGroupList.Where(x => x.SignalMark == "BUTTON").Select(x => x.ID).ToArray();
            var TriaItemList = GolbalVariable.TriaItemList.Where(s => triaGroups.Contains(s.GroupID)).ToList();
            var TriaItems = GolbalVariable.TriaItemList.Where(s => triaGroups.Contains(s.GroupID)).Select(x => x.SignalMark).ToArray();

            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;

            if (model != null)
            {
                option.Text = "工程设置(" + model.WorkName + ")";
                option.AddText("WorkAddress", "岗位输入地址", model.WorkAddress, true);
                option.AddText("WorkName", "岗位工程名称", model.WorkName, false);
                option.AddCombobox("SignalMark", "信号源标识", TriaItemList, "SignalMark", "SignalMark", model.SignalMark);
            }
            else
            {
                option.Text = "新增工程";
                option.AddText("WorkAddress", "岗位输入地址", null, true);
                option.AddText("WorkName", "岗位工程名称", null, false);
                option.AddCombobox("SignalMark", "信号源标识", TriaItems);
            }

            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                if (model != null)
                {
                    model.WorkAddress = frm["WorkAddress"].ToString();
                    model.WorkName = frm["WorkName"].ToString();
                    if (frm["SignalMark"] != null)
                        model.SignalMark = frm["SignalMark"].ToString();
                    if (projectSetBLL.Update(model).IsSuccess)
                        UIMessageTip.ShowOk("设置成功");
                    else
                        UIMessageBox.ShowError("设置失败！");
                    LogObj.Info("工程设置(" + model.WorkName + ")");
                }
                else
                {
                    model = new ProjectSetModel();
                    switch (dgvName)
                    {
                        case "dgv1":
                            model.AreaType = 1;
                            model.AreaName = "F-DR";
                            break;
                        case "dgv2":
                            model.AreaType = 2;
                            model.AreaName = "T-SPS";
                            break;
                        case "dgv3":
                            model.AreaType = 3;
                            model.AreaName = "IP-SPS";
                            break;
                        case "dgv4":
                            model.AreaType = 4;
                            model.AreaName = "F-SPS";
                            break;
                        case "dgv5":
                            model.AreaType = 5;
                            model.AreaName = "DR-SPS";
                            break;
                    }
                    model.WorkAddress = frm["WorkAddress"].ToString();
                    model.WorkName = frm["WorkName"].ToString();
                    model.SignalMark = TriaItems[int.Parse(frm["SignalMark"].ToString())];
                    if (projectSetBLL.Add(model).IsSuccess)
                        UIMessageTip.ShowOk("新增成功");
                    else
                        UIMessageBox.ShowError("新增失败！");
                    LogObj.Info("新增工程(" + model.WorkName + ")");
                }
                LoadDgv();
            }
            frm.Dispose();
        }

        /// <summary>
        /// 工程删除
        /// </summary>
        /// <param name="entityMaterialDirection"></param>
        private void ProjectDelect(ProjectSetModel model)
        {
            if (projectSetBLL.Delete(model).IsSuccess)
                UIMessageTip.ShowOk("删除成功");
            else
                UIMessageBox.ShowError("删除失败！");
            LoadDgv();
        }
    }
}
